//
//  ContentManager.m
//  Player
//
//  Created by howell on 16/3/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ContentManager.h"
#import "AFNetworking.h"
#import "ObjectAL.h"
#import "MujiDubNetworkAPI.h"

@implementation ContentManager

+ (ContentManager *)sharedManager
{
    static ContentManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
                  {
                      sharedManager = [ContentManager alloc];
                  });
    
    return sharedManager;
}


-(void)getDialogCompleteMetaDataWithID:(NSString*)dialogId
                          success:(ContentManagerSuccessBlock)successBlock
                          failure:(ContentManagerFailureBlock)failureBlock
{
    
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [arr lastObject];
    
    NSString *metadataCachePath = [cachePath stringByAppendingPathComponent:@"mujiMetadataCache"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *outError = nil;
    if(![fileManager fileExistsAtPath:metadataCachePath]){
        
        [fileManager createDirectoryAtPath:metadataCachePath withIntermediateDirectories:YES attributes:nil error:&outError];
    }

    
    NSString *fileName = [NSString stringWithFormat:@"%@_metadata.plist", dialogId];
    NSString *filePath = [metadataCachePath stringByAppendingPathComponent:fileName];
    
    NSLog(@"%@", filePath);
    
    if([fileManager fileExistsAtPath:filePath])
    {
        //NSLog(@"data ---------- %@", [NSArray arrayWithContentsOfFile:filePath]);

        NSError *error = nil;
        NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
        
        if (error) {
            
            NSLog(@"error ---------- %@", error);
            
            if (failureBlock)
            {
                failureBlock(error);
            }
            

        }else {
            
            
            if(successBlock)
            {
                successBlock(array);
            }
        }
        return;
    }
    
  //  NSString *path = [NSString stringWithFormat:@"%@%@?complete=true&partnerKey=5eb63bbbe01eeed093cb22bb8f5acdc3",kDialogLines_URL,dialogId];
    
    //20170616171911_644
    NSString *path  = [NSString stringWithFormat:@"%@%@.json", kMetaDataURL, dialogId];
    
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //NSLog(@"---------%@", path);
    //NSString *path = [NSString stringWithFormat:@"%@%@?complete=true",kDialogLines_URL,dialogId];
    
    [[AFHTTPSessionManager manager] GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [responseObject writeToFile:filePath atomically:YES];
        
        if(successBlock)
        {
            successBlock(responseObject);
        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failureBlock)
        {
            failureBlock(error);
        }

        
    }];
    
    
//    [[AFHTTPSessionManager manager] GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        
//        
//        [responseObject writeToFile:filePath atomically:YES];
//        
//        if(successBlock)
//        {
//            successBlock(responseObject);
//        }
//        
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        if (failureBlock)
//        {
//            failureBlock(error);
//        }
//    }];
}

- (BOOL)playEffectForCoreLibraryFile:(NSString *)file
                              ofType:(NSString *)extension
{
    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:file ofType:extension];
    
    return [self playEffectForAudioFilePath:audioFilePath];
}

- (BOOL)playEffectForAudioFilePath:(NSString *)audioFilePath
{
    
    if (audioFilePath)
    {
        //[[OALSimpleAudio sharedInstance] playEffect:audioFilePath];
        return YES;
    }
    else
    {
        return NO;
    }
}

@end
